import math
from ctypes import *

libad = cdll.LoadLibrary("/home/pi/Documents/rmo.so")

'''
将原来的St开头的文件（图像分析？）算法集中到本类中，原来太分散.
定义的方法均为静态方法，静态方法说明see:https://www.cnblogs.com/bingoTest/p/10518086.html
'''


class RecognitionAlgorithm:
    '''
    (功能描述未知.待定.....)
    '''

    @staticmethod
    def sum_col(A):
        size_A = A.shape
        a = size_A[1]
        b = size_A[0]
        sum_c = []
        for i in range(a):
            tem = 0
            for j in range(b):
                tem += A[j, i]
            sum_c.append(tem)
        return sum_c

    '''
    (功能描述未知.待定.....)
    '''

    @staticmethod
    def filter_G(A):
        x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
        for i in range(len(x)):
            x[i] = math.e ** (-x[i] * x[i] / 2)
        s = sum(x)
        b = len(A) - 1
        b = libad.rm(b)
        temp = 0
        filter_A = []
        for i in range(5, b - 5):
            temp = (A[i - 5] * x[0] + A[i - 4] * x[1] + A[i - 3] * x[2] + A[i - 2] * x[3] + A[i - 1] * x[4] + A[i] * x[
                5] + A[i + 1] * x[6] + A[i + 2] * x[7] + A[i + 3] * x[8] + A[i + 4] * x[9] + A[i + 5] * x[10]) / s
            filter_A.append(temp)
        return filter_A

    '''
    (功能描述未知.待定.....)
    '''

    @staticmethod
    def diff_fb(B):
        c = len(B) - 1
        c = libad.rm(c)
        diff = []
        for i in range(c):
            mtem = B[i] - B[i - 1]
            diff.append(mtem)
        diff[0] = 0
        return diff

    '''
    (功能描述未知.待定.....)
    '''

    @staticmethod
    def p_count(Dif, apnum):
        sort_Dif = sorted(Dif)
        Re = sort_Dif[::-1]
        ave_trouths = sum(sort_Dif[10:apnum + 10]) / (apnum)
        ave_peaks = sum(Re[10:apnum + 10]) / (apnum)
        A01 = (ave_peaks) / 3.5
        A02 = (ave_trouths) / 3.5
        N_Dif = []
        for x in Dif:
            if A02 < x < A01:
                x = 0
                N_Dif.append(x)
            else:
                N_Dif.append(x)
        peaks = 0
        troughs = 0
        troughs = libad.rm(troughs)
        for idx in range(1, len(N_Dif) - 1):
            if N_Dif[idx - 1] > N_Dif[idx] < N_Dif[idx + 1]:
                troughs = troughs + 1
            if N_Dif[idx - 1] < N_Dif[idx] > N_Dif[idx + 1]:
                peaks = peaks + 1
        Bi_Dif = []
        for x in N_Dif:
            if x > 0:
                x = 1
                Bi_Dif.append(x)
            elif x < 0:
                x = -1
                Bi_Dif.append(x)
        diff_Bi = []
        for idx in range(1, len(Bi_Dif)):
            biTem = Bi_Dif[idx] - Bi_Dif[idx - 1]
            diff_Bi.append(biTem)
        P_Peak = diff_Bi.count(2) + 1
        P_Trough = diff_Bi.count(-2) + 1
        P_Pount_N = [peaks, troughs, P_Peak, P_Trough]
        return P_Pount_N